package parkingManage.utils;

import java.util.List;
import java.util.Scanner;

import Util.TestUtil;
import parkingManage.dao.CarDao;
import parkingManage.dao.impl.CarDaoImpl;
import parkingManage.model.CarModel;

public class CarUtils {

	static Scanner sc = new Scanner(System.in);

	public void start() {
		while (true) {
			System.out.println("===================================");
			System.out.println("请选择：");
			System.out.println("1-查询所有车辆信息");
			System.out.println("2-根据车牌号查询车辆信息");
			System.out.println("3-退出");
			System.out.println("===================================");

			int key = TestUtil.abnormal();
			if (key == 3)
				break;

			switch (key) {
			case 1:
				selectAll();
				break;
			case 2:
				selectByLic();
				break;
			default:
				break;
			}
			System.out.println("点击回车继续...");
			sc.nextLine();
		}
	}

	/**
	 * 查询所有车辆信息
	 */
	public void selectAll() {
		CarDao car = new CarDaoImpl();
		List<CarModel> cars = car.selectAllCar();
		System.out.println("============全部车辆信息============");
		for (CarModel carModel : cars) {
			info(carModel);
		}
	}

	/**
	 * 根据车牌号查询车辆信息
	 */
	public void selectByLic() {
		System.out.print("请输入您要查询的车牌号：");
		String license = sc.nextLine();

		CarDao carDao = new CarDaoImpl();
		CarModel car = carDao.selectByLicense(license);

		info(car);
	}

	/**
	 * 车辆详细信息
	 * 
	 * @param cars
	 */
	private void info(CarModel car) {
		System.out.println();
		if (car == null) {
			System.out.println("**没有查到相关车辆...");
			return;
		}
		System.out.println("-----------------------------------");
		System.out.println("车牌号：" + car.getLicense());
		System.out.println("车辆高度：" + car.getHeight() + "毫米");
		System.out.println("-----------------------------------");
	}

	public void addCar() {
		System.out.println("===============添加车辆(停车)===============");
		System.out.print("请输入车牌号：");
		String license = sc.nextLine();

		System.out.print("请输入车辆高度（毫米）：");
		int height = TestUtil.abnormal();

		if (height > 1500) {
			System.out.println("对不起，您的车辆高度超过本停车场的最高限制，无法停车！");
			return;
		}

		CarModel car = new CarModel(license, height);

		// 添加停车记录
		RecordUtils record = new RecordUtils();
		record.addRecord(license);

		// 添加车辆信息
		CarDao carDao = new CarDaoImpl();
		int num = carDao.saveCar(car);

		if (num >= 1) {
			System.out.println("已成功停入一辆车...");
			info(car);
			return;
		}
		System.out.println("添加失败！");
	}

	public static void main(String[] args) {
		CarUtils carUtils = new CarUtils();
		carUtils.start();
	}
}
